﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Data
{
    public class AccountData
    {
        Connect cn = new Connect();
        AccountsEntity acE = new AccountsEntity();

        //Cach 1
        //public static DataTable getBalance(CardsEntity card)
        //{
        //    card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
        //    DataTable dt = DataAccess.ExeStoredWithParamToDataTable("CheckBalance", "@CardNo", card.CardNo);
        //    return dt;
        //}
        public decimal getAccount(AccountsEntity acE)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "AccountID";
            value[0] = acE.AccountID;
            string sql = "dbo.AccountsSelectByID";
            DataTable dt = new DataTable();
            dt = cn.GetData(sql, name, value, Npara);
            return (decimal)dt.Rows[0]["Balance"];
        }
        public AccountsEntity GetAccount(AccountsEntity acE)
        {
            int Npara=1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@AccountID";
            value[0] = acE.AccountID;
            string sql = "dbo.AccountsSelectByID";
            DataTable dt = new DataTable();
            dt= cn.GetData(sql, name, value, Npara);
            AccountsEntity accE = new AccountsEntity
            {
                AccountID = (int)dt.Rows[0]["AccountID"],
                AccountNo = dt.Rows[0]["AccountNo"].ToString(),
                Balance = (decimal)dt.Rows[0]["Balance"],
                CustID = (int)dt.Rows[0]["CustID"],
                ODID = (int)dt.Rows[0]["ODID"],
                WDID = (int)dt.Rows[0]["WDID"]
            };
            return accE;
        }
        public void Transfer(int accountID, int accountID1,decimal amount, string cardNo)
        { 
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("dbo.Transfers", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Value = amount;
                    cmd.Parameters.Add("@AccountID", SqlDbType.Int).Value = accountID;
                    cmd.Parameters.Add("@AccountID1", SqlDbType.Int).Value = accountID1;
                    cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = cardNo;
                    cmd.Parameters.Add("@LogDate", SqlDbType.DateTime).Value =DateTime.Now;
                    cmd.Parameters.Add("@Details", SqlDbType.NVarChar).Value = "suc";
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
        public void UpdateAccount( AccountsEntity acE )
        {
            AccountData accD = new AccountData();
            int Npara = 6;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@AccountID";
            value[0] = acE.AccountID;
            name[1] = "@AccountNo";
            value[1] = accD.GetAccount(acE).AccountNo;
            name[2] = "@Balance";
            value[2] = acE.Balance;
            name[3] = "@CustID";
            value[3] = accD.GetAccount(acE).CustID;
            name[4] = "@ODID";
            value[4] = accD.GetAccount(acE).ODID;
            name[5] = "@WDID";
            value[6] = accD.GetAccount(acE).WDID;
            string sql = "dbo.AccountsUpDate";
            cn.NonQuery(sql, name, value, Npara);
        }
        public DataTable GetAllAccount()
        {
            string sql = "dbo.AccountsSelectAll";
            DataTable dt = new DataTable();
            dt = cn.GetData(sql);
            return dt;
        }
    }
}
